//
//  PPUserAccount.m
//  pickpic
//
//  Created by 胡文广 on 2017/3/23.
//  Copyright © 2017年 MicroDream. All rights reserved.
//

#import "PPUserAccount.h"
//#import <Security/Security.h>
@implementation PPUserAccount

Archive_Runtime(PPUserAccount);
+(PPUserAccount *)currentUser{
    
//    static PPUserAccount *user = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        user = [[PPUserAccount alloc]init];
//    });
    
    PPUserAccount *user;
    if ([NSKeyedUnarchiver unarchiveObjectWithFile:kUserInfo]) {
     user = [NSKeyedUnarchiver unarchiveObjectWithFile:kUserInfo];
    }else{
        user = [[self alloc] init];
    }
    
    return user;
}

- (void)saveAccount{

    [NSKeyedArchiver archiveRootObject:self toFile:kUserInfo];
}
+ (void)updateAccountInfoKey:(NSString *)key value:(NSString *)value{

    if([[NSKeyedUnarchiver unarchiveObjectWithFile:kUserInfo] isKindOfClass:[PPUserAccount class]]){
    
        PPUserAccount *acc = [NSKeyedUnarchiver unarchiveObjectWithFile:kUserInfo];
        
        NSMutableDictionary *dict = [acc yy_modelToJSONObject];
 
        dict[key] = value;
        
        PPUserAccount *userM = [PPUserAccount yy_modelWithDictionary:[dict copy]];
            
        [NSKeyedArchiver archiveRootObject:userM toFile:kUserInfo];
        
    }else{
    
        PPLog(@"更新用户信息失败");
    };
    
}
+ (void)clearAccount{
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager removeItemAtPath:kUserInfo error:nil];
    
}
- (NSString *)avatarURLString {
    if(self.avatar.length == 0 || self.avatarHost.length == 0) {
        return nil;
    }
    
    if([self.avatar hasPrefix:@"http://"]) {
        return self.avatar;
    } else if([self.avatar hasPrefix:@"http:/"]) {
        return [self.avatar stringByReplacingOccurrencesOfString:@"http:/" withString:@"http://"];
    } else if([self.avatar hasPrefix:@"http:"]) {
        return [self.avatar stringByReplacingOccurrencesOfString:@"http:" withString:@"http://"];
    }
    return [NSString stringWithFormat:@"%@%@", self.avatarHost, self.avatar];
}

- (void)registerFont{
    NSString *string = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *tempFileList = [[NSArray alloc] initWithArray:[fileManager contentsOfDirectoryAtPath:string error:nil]];
    for (NSString *fontP in tempFileList)
    {
        if([fontP rangeOfString:@".TTF"].location != NSNotFound)
        {
            [fontP registerFontPath];
        }
        
    }
}

@end
